package com.weixin.paleituo.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weigan.loopview.LoopView;
import com.weixin.paleituo.R;
import com.weixin.paleituo.eventbus.EventBusEvent;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Author yichao
 * Time  2018/4/23 16:58
 * Dest  选择时间
 */
public class SelectTimePopuWindow extends PopupWindow{


    private final List<String> mDateStrList;
    private final List<String> mTimeStrList;
    private LoopView mLoopViewDate;
    private LoopView mLoopViewTime;
    private RelativeLayout mRlRoot;
    private LinearLayout mLlcontetRoot;
    private TextView mTvCancel;
    private TextView mTvSure;
    private String TAG="SelectTimePopuWindow";

    public SelectTimePopuWindow(Context context, List<String> dateStrList, List<String> timeStrList) {
        super(context);
        mDateStrList = dateStrList;
        mTimeStrList = timeStrList;


        init(context,dateStrList,timeStrList);

    }


    private void init(Context context, List<String> dateStrList, List<String> timeStrList) {

        View rootView= UIUtils.inflate(context, R.layout.select_time_popu);

        mLoopViewDate = rootView.findViewById(R.id.loopView_date_select_time_popu);
        mLoopViewTime = rootView.findViewById(R.id.loopView_time_select_time_popu);

        mRlRoot = rootView.findViewById(R.id.rl_root_select_time_popu);
        mLlcontetRoot = rootView.findViewById(R.id.ll_contentroot_select_time_popu);
        mTvCancel = rootView.findViewById(R.id.tv_cancel_select_time_popu);
        mTvSure = rootView.findViewById(R.id.tv_sure_select_time_popu);

        setBackgroundDrawable(new ColorDrawable(0x90000000));
        // TODO: 2016/5/17 设置可以获取焦点
        setFocusable(true);
        // TODO: 2016/5/17 设置可以触摸弹出框以外的区域
        setOutsideTouchable(true);
        // TODO：更新popupwindow的状态
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(rootView);

        mLoopViewDate.setItems(dateStrList);
        mLoopViewTime.setItems(timeStrList);
        //设置初始位置
        mLoopViewDate.setInitPosition(0);
        mLoopViewTime.setInitPosition(0);

        mLoopViewDate.setNotLoop();
        mLoopViewTime.setNotLoop();

        initEvent();
    }

    /**
     * 事件
     */
    private void initEvent() {

        mRlRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mLlcontetRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }

        });
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                //保存
                int selectedDate = mLoopViewDate.getSelectedItem();

                int selectedTime = mLoopViewTime.getSelectedItem();
                String dateStr = mDateStrList.get(selectedDate);
                String timeStr = mTimeStrList.get(selectedTime);


                EventBusEvent eventBusEvent=new EventBusEvent();
                eventBusEvent.setTime(dateStr+timeStr);
                EventBus.getDefault().post(eventBusEvent);


            }
        });

    }



}
